package cn.wlinker.video.monitor.domain;

import cn.hutool.core.util.ObjectUtil;
import lombok.Data;

import java.io.Serializable;

/**
 * 分页查询实体类
 *
 * @author Lion Li
 */

@Data
public class PageQuery implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 分页大小
     */
    private Integer pageSize;

    /**
     * 当前页数
     */
    private Integer pageNum;

    /**
     * 当前记录起始索引 默认值
     */
    public static final int DEFAULT_PAGE_NUM = 1;

    /**
     * 每页显示记录数 默认值 默认查50
     */
    public static final int DEFAULT_PAGE_SIZE = 50;

    public <T> Page<T> build() {
        Integer pageNum = ObjectUtil.defaultIfNull(getPageNum(), DEFAULT_PAGE_NUM);
        Integer pageSize = ObjectUtil.defaultIfNull(getPageSize(), DEFAULT_PAGE_SIZE);
        if (pageNum <= 0) {
            pageNum = DEFAULT_PAGE_NUM;
        }
        return new Page<>(pageNum, pageSize);
    }

    private void init(Integer pageSize, Integer pageNum) {
        this.pageSize = ObjectUtil.defaultIfNull(pageSize, DEFAULT_PAGE_SIZE);
        this.pageNum = ObjectUtil.defaultIfNull(pageNum, DEFAULT_PAGE_NUM);
        if (pageNum != null && pageNum <= 0) {
            this.pageNum = DEFAULT_PAGE_NUM;
        }
    }

    public PageQuery(Integer pageSize, Integer pageNum) {
        init(pageSize, pageNum);
    }

    public PageQuery() {
        init(null, null);
    }
}
